package edu.nanyang.controller;

import edu.nanyang.domain.OptionInfo;
import edu.nanyang.service.OptionInfoService;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin/option")
@Api(tags = "选项操作接口")

public class OptionInfoController extends BaseController{
    @Resource
    private OptionInfoService optionInfoService;

    @GetMapping("/getByNumIdAndType")
    public String getByNumIdAndType(int questionNumId, String optionType) {
        OptionInfo optionInfo = optionInfoService.getByNumIdAndType(questionNumId, optionType);
        if(null==optionInfo){
            optionInfo=new OptionInfo();
            optionInfo.setQuestionNumId(questionNumId);
            optionInfo.setOptionType(optionType);
        }
        request.setAttribute("option_info", optionInfo);
        return "/admin/option/option_edit";
    }

    @PostMapping("/updatesave")
    public String updateSave(OptionInfo optionInfo) {
        if(optionInfo.getId()==null || optionInfo.getId().equals("")){
            int result=optionInfoService.add(optionInfo);
            if (result>0) {
                request.setAttribute("tip_info", "添加选项成功");
            } else {
                request.setAttribute("tip_info", "添加选项失败");
            }
        }else {
            int result = optionInfoService.update(optionInfo);
            if (result>0) {
                request.setAttribute("tip_info", "修改选项成功");
            } else {
                request.setAttribute("tip_info", "修改选项失败");
            }
        }
        return "result";
    }

}
